// greedy alg.
// 1. cost=prices[i]+fee, compare with prices[j]
// less: profit=prices[j]-(prices[i]+fee), meanwhile, cost=prices[j], go on
// 2. cost>prices[j]+fee: update cost=prices[j]+fee
// time: O(n), space: O(1)
func maxProfit(prices []int, fee int) int {
	cost := prices[0] + fee
	profit := 0
	for _, p := range prices[1:] {
		if cost < p {
			profit += p - cost
			cost = p
		} else if cost > p+fee {
			cost = p + fee
		}
	}
	return profit
}